import { USER_LOGIN, INIT_USER, LOGIN_OUT } from './mutations-types.js'
export default {
    state: {
        //登陆状态
        loginStatus: false,
        //token
        token: null,
        //用户信息
        userInfo: {

        }
    },
    getters: {

    },
    mutations: {
        //登录传值
        [USER_LOGIN](state, user) {
            //登陆成功
            state.loginStatus = true
            state.token = user.token

            //登陆成功后传递的用户信息的对象
            state.userInfo = user
                //持久化储存 本地存储
            localStorage.setItem('UserInfo', JSON.stringify(user))
            console.log(state, user)
        },
        //读取用户信息（刷新后也不会消失 本地存储）
        [INIT_USER](state) {
            let userInfo = JSON.parse(localStorage.getItem('UserInfo'))
            if (userInfo) {
                state.loginStatus = true
                state.token = userInfo.token
                state.userInfo = userInfo
            }

        },
        //退出登录(清除本地存储)
        [LOGIN_OUT](state) {
            state.loginStatus = false
            state.token = null
            state.userInfo = {}
            localStorage.removeItem('UserInfo')
        }
    },
    actions: {

    },
}